package csci4448.android.workouttracker;

import csci4448.android.workouttracker.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RecordTabActivity extends Activity implements RecordTabControllerEventListener  {

	private RecordTabController _recorder;
	private TextView DistanceView;
	private TextView TimeView;
	private TextView AverageSpeedView;
	
	public RecordTabActivity(){
		super();
	}
	
	public class RecordTabUpdateRunnable implements Runnable {

		private RecordTabControllerEvent _updateEvent;
		public RecordTabUpdateRunnable(RecordTabControllerEvent UpdateEvent)
		{
			_updateEvent = UpdateEvent;
		}
		public void run() {
			Session InProgressSession = _updateEvent.getInProgressSession();
			DistanceView.setText(InProgressSession.getDistanceString());
			TimeView.setText(InProgressSession.getDurationString());
			AverageSpeedView.setText(InProgressSession.getSpeedString());
		}
		
	}
	public void handleGPSRecorderEvent(RecordTabControllerEvent e) {
		DistanceView.post(new RecordTabUpdateRunnable(e));
		TimeView.post(new RecordTabUpdateRunnable(e));
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_tab);
		TimeView = (TextView)findViewById(R.id.TimeElapsedValue);
		DistanceView = (TextView)findViewById(R.id.TotalDistanceValue);
		AverageSpeedView = (TextView)findViewById(R.id.AverageSpeedValue);
		_recorder = new RecordTabController(this.getApplicationContext());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_record_tab, menu);
        return true;
    }

    public void SetInterfaceByState(RecordingState state){
    	final Button StartFinishButton = (Button) findViewById(R.id.StartFinish);
    	final Button PauseResumeButton = (Button) findViewById(R.id.PauseButton2);
    	final Button CancelButton = (Button) findViewById(R.id.CancelButton);
    	switch(state)
    	{
	    	case Paused:
	    		StartFinishButton.setText(string.FinishButtonText);
	    		StartFinishButton.setVisibility(View.VISIBLE);
	    		CancelButton.setVisibility(View.VISIBLE);
	    		PauseResumeButton.setText(string.ResumeButtonText);
	    		PauseResumeButton.setVisibility(View.VISIBLE);
	    		break;
	    	case Recording:
	    		StartFinishButton.setText(string.FinishButtonText);
	    		StartFinishButton.setVisibility(View.VISIBLE);
	    		CancelButton.setVisibility(View.VISIBLE);
	    		PauseResumeButton.setText(string.PauseButtonText);
	    		PauseResumeButton.setVisibility(View.VISIBLE);
	    		_recorder.addEventListener(this);
	    		break;
	    	case Cancelled:
	    	case Idle:
	    	case Finished:
	    		StartFinishButton.setText(string.StartButtonText);
	    		StartFinishButton.setVisibility(View.VISIBLE);
	    		CancelButton.setVisibility(View.INVISIBLE);
	    		PauseResumeButton.setText(string.PauseButtonText);
	    		PauseResumeButton.setVisibility(View.INVISIBLE);
	    		_recorder.removeEventListener(this);

	    		break;
    	}
    }
    
    public void onStartFinishClick(View view){
    	if(_recorder.getRecordingState() == RecordingState.Recording ||
    			_recorder.getRecordingState() == RecordingState.Paused)
    		finishSessionDialog(view);
    	else
			SetInterfaceByState(_recorder.ToggleStartFinishState());
    }
    
    public void onCancelClick(View view){
    	if(_recorder.getRecordingState() == RecordingState.Recording || 
    			_recorder.getRecordingState() == RecordingState.Paused)
    		cancelSessionDialog(view);
    	else
    		SetInterfaceByState(_recorder.CancelRecording());
    }
    
    public void onPauseResumeClick(View view){
    	SetInterfaceByState(_recorder.TogglePauseResumeState());
    }

    /**
     * Popup a dialog box asking the user if he/she wishes to
     * finish and save the current running session
     * 
     * @param view
     * The view from which the finish dialog box is to be created
     */
    public void finishSessionDialog(View view) {
    	// if already paused, leave it paused
    	if(_recorder.getRecordingState() != RecordingState.Paused)
    		_recorder.TogglePauseResumeState();
    	
    	final EditText input = new EditText(this);
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	alert.setTitle("Finish Session");
    	alert.setMessage("Input Session Name:");
    	input.setText(_recorder.getInProgressSession().getStartTime(), TextView.BufferType.EDITABLE);
    	alert.setView(input);
    	alert.setNegativeButton("Resume Session", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface finishDialog, int id) {
				if (_recorder.getRecordingState() != RecordingState.Paused)
		    		_recorder.TogglePauseResumeState();
				finishDialog.cancel();
			}
    	});
    	alert.setPositiveButton("Finish & Save", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface finishDialog, int id) {
				SetInterfaceByState(_recorder.ToggleStartFinishState());
				_recorder.saveSession(input.getText().toString());
				finishDialog.cancel();
			}
    	});
    	alert.show();
    }
    
    /**
     * Popup a dialog box asking the user if he/she wishes to
     * cancel and discard the running session
     * 
     * @param view
     * The view from which the cancel dialog box is to be created
     */
    public void cancelSessionDialog(View view) {
    	// if already paused, leave it paused
    	if (_recorder.getRecordingState() != RecordingState.Paused)
    		_recorder.TogglePauseResumeState();
    	
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	alert.setTitle("Cancel Session");
    	alert.setMessage("Are you sure you want to cancel your session?");
    	alert.setNegativeButton("Resume Session", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface finishDialog, int id) {
				if (_recorder.getRecordingState() != RecordingState.Paused)
		    		_recorder.TogglePauseResumeState();
				finishDialog.cancel();
			}
    	});
    	alert.setPositiveButton("Discard Session", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface finishDialog, int id) {
				SetInterfaceByState(_recorder.CancelRecording());
				finishDialog.cancel();
			}
    	});
    	alert.show();
    }
}
